"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeAppendFile = exports.mkdirCheckExists = exports.parseTime = void 0;
var fs_1 = __importDefault(require("fs"));
var path_1 = __importDefault(require("path"));
// 格式化时间戳
var parseTime = function (time, cFormat) {
    if (cFormat === void 0) { cFormat = '{y}-{m}-{d} {h}:{i}:{s}'; }
    if (!time)
        return '';
    var format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}';
    var date;
    if (typeof time === 'object') {
        date = time;
    }
    else {
        if ((typeof time === 'string')) {
            if ((/^[0-9]+$/.test(time))) {
                time = parseInt(time);
            }
            else {
                time = time.replace(/-/gm, '/');
            }
        }
        if ((typeof time === 'number') && (time.toString().length === 10)) {
            time = time * 1000;
        }
        date = new Date(time);
    }
    var formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay()
    };
    var timeStr = format.replace(/{([ymdhisa])+}/g, function (result, key) {
        var value = formatObj[key];
        // Note: getDay() returns 0 on Sunday
        if (key === 'a') {
            return ['日', '一', '二', '三', '四', '五', '六'][value];
        }
        return value.toString().padStart(2, '0');
    });
    return timeStr;
};
exports.parseTime = parseTime;
// 检查文件夹是否存在再创建文件夹
var mkdirCheckExists = function (path) {
    if (path === void 0) { path = ''; }
    if (!path)
        return;
    if (!fs_1.default.existsSync(path)) {
        fs_1.default.mkdirSync(path);
    }
};
exports.mkdirCheckExists = mkdirCheckExists;
var writeAppendFile = function (savePath, fileName, val) {
    if (savePath === void 0) { savePath = ''; }
    if (fileName === void 0) { fileName = ''; }
    if (val === void 0) { val = ''; }
    return new Promise(function (resolve, reject) {
        if (!savePath || !fileName || !val)
            return reject(null);
        // 创建文件夹
        (0, exports.mkdirCheckExists)(savePath);
        // 完整文件路径
        var filePath = path_1.default.posix.join(savePath, fileName);
        // 判断是否存在
        if (fs_1.default.existsSync(filePath)) {
            // 存在插入内容
            fs_1.default.appendFile(filePath, val, { encoding: 'utf-8' }, function (err) { return err ? reject(err) : resolve(null); });
        }
        else {
            // 不存在新建写入内容
            fs_1.default.writeFile(filePath, val, { encoding: 'utf-8', flag: 'w' }, function (err) {
                if (err) {
                    reject(err);
                }
                else {
                    (0, exports.writeAppendFile)(savePath, fileName, val);
                    resolve(null);
                }
            });
        }
    });
};
exports.writeAppendFile = writeAppendFile;
